package com.elinkway.infinitemovies.share;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.text.TextUtils;
import android.widget.Toast;

import com.elinkway.infinitemovies.bean.ShareContent;
import com.elinkway.infinitemovies.bigdata.BigDataApi;
import com.elinkway.infinitemovies.ui.activity.commonwebview.CommonWebViewActivity;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.UmengEventPoster;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Map;

public class ShareUtils {
    public static enum ShareType {
        WEIBO, QZONE, WEIXIN, FRIEND,
    }

    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        int i;
        int j;
        if (bmp.getHeight() > bmp.getWidth()) {
            i = bmp.getWidth();
            j = bmp.getWidth();
        } else {
            i = bmp.getHeight();
            j = bmp.getHeight();
        }

        Bitmap localBitmap = Bitmap.createBitmap(i, j, Bitmap.Config.RGB_565);
        Canvas localCanvas = new Canvas(localBitmap);

        while (true) {
            localCanvas.drawBitmap(bmp, new Rect(0, 0, i, j), new Rect(0, 0, i, j), null);
            if (needRecycle)
                bmp.recycle();
            ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
            localBitmap.compress(Bitmap.CompressFormat.JPEG, 100, localByteArrayOutputStream);
            localBitmap.recycle();
            byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
            try {
                localByteArrayOutputStream.close();
                return arrayOfByte;
            } catch (Exception e) {
                // F.out(e);
            }
            i = bmp.getHeight();
            j = bmp.getHeight();
        }
    }

    public static byte[] InputStreamToByte(InputStream is) {
        try {
            ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
            int ch;
            while ((ch = is.read()) != -1) {
                bytestream.write(ch);
            }
            byte imgdata[] = bytestream.toByteArray();
            bytestream.close();
            return imgdata;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void loadImage(String img_url, ShareType type, ImageLoadingListener listener) {
        ImageLoader.getInstance().loadImage(img_url, listener);
    }

    public static String getString(SHARE_MEDIA type) {
        switch (type) {
            case WEIXIN:
                return "weixin";
            case WEIXIN_CIRCLE:
                return "wx_moments";
            case SINA:
                return "weibo";
            case QZONE:
                return "qzone";
            case QQ:
                return "qq";
        }
        return "unknow";
    }

    public static void shareToThird(final Activity activity, final ShareContent content, final SHARE_MEDIA type){
        ShareAction thirdShare =new ShareAction(activity)
                .setPlatform(type)
                .setCallback(new UMShareListener() {
                    @Override
                    public void onResult(SHARE_MEDIA platform) {
                        if (activity instanceof CommonWebViewActivity) {
                            UmengEventPoster.postH5Share(content.getLink(), content.getH5Title(), getString(type), "Y", "events");
                        }
                        if(content.getDbMap() != null){
                            Map<String, String> dbMap = content.getDbMap();
                            dbMap.put("ap", "sharePlatform="+platform);
                            BigDataApi.bigDataCommonReport(dbMap, activity);
                        }
                        Toast.makeText(activity, platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(SHARE_MEDIA platform, Throwable t) {
                        if (activity instanceof CommonWebViewActivity) {
                            UmengEventPoster.postH5Share(content.getLink(), content.getH5Title(), getString(type), "N", "events");
                        }
                        Toast.makeText(activity, platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA platform) {
                        if (activity instanceof CommonWebViewActivity) {
                            UmengEventPoster.postH5Share(content.getLink(), content.getH5Title(), "cancel", "N", "events");
                        }
                        Toast.makeText(activity, platform + " 分享取消了", Toast.LENGTH_SHORT).show();
                    }
                })
                .withMedia(new UMImage(activity, content.getImageUrl()));
                if (!TextUtils.isEmpty(content.getTitle())) {
                    thirdShare.withTitle(content.getTitle());
                }
                if(SHARE_MEDIA.SINA == type){
                    thirdShare.withText(content.getContent() + " " + content.getLink());
                }else{
                    LogUtils.e("share", "share " + content.getContent());
                    thirdShare.withText(content.getContent())
                    .withTargetUrl(content.getLink());
                }
        thirdShare.share();
    }
}
